package com.weike.yuanding.strategy.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.weike.yuanding.strategy.ChatStrategy;
import com.weike.yuanding.vo.ChatResVo;
import com.weike.yuanding.vo.ChatVo;
import io.reactivex.Flowable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @ClassName: AliYunStrategy
 * @Author: YuanDing
 * @Date: 2025/4/23 21:05
 * @Description:
 * 阿里云百炼平台大模型
 */

@Slf4j
@Component("aliyun")
public class AliYunStrategy implements ChatStrategy {

    @Autowired
    private Map<String , GenerationParam> generationParamMap;

    private static final StringBuilder reasoningContent = new StringBuilder();
    private static final StringBuilder finalContent = new StringBuilder();

    @Override
    public void execute(List<Message> messageList , ChatVo chatVo, Consumer<ChatResVo> callBack) throws NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        GenerationParam generationParam = generationParamMap.get(chatVo.getModel());


        generationParam.setMessages(messageList);

        Flowable<GenerationResult> result = gen.streamCall(generationParam);

        HashMap<String, Object> tipsMap = new HashMap<>();
        result.subscribe(
                resultMessage -> {
                    String reasoning = resultMessage.getOutput().getChoices().get(0).getMessage().getReasoningContent();
                    String content = resultMessage.getOutput().getChoices().get(0).getMessage().getContent();
                    if (!StrUtil.isBlankIfStr(reasoning) && !reasoning.isEmpty() && !tipsMap.containsKey("sign")) {
                        callBack.accept(ChatResVo.builder()
                                .reasoningContent(reasoning)
                                .type("0")
                                .build());
                    }

                    if (!content.isEmpty()) {
                        tipsMap.put("sign" , "1");
                        callBack.accept(ChatResVo.builder()
                                .content(content)
                                .type("1")
                                .sessionId(chatVo.getSessionId())
                                .build());
                    }
                },
                error -> {

                    callBack.accept(ChatResVo.builder()
                            .isDone(true)
                            .build());
                    error.printStackTrace(); // 打印具体错误
                },
                () -> {
                    callBack.accept(ChatResVo.builder()
                            .isDone(true)
                            .build());
                }
        );
    }

    private void handleGenerationResult(GenerationResult message) {
        String reasoning = message.getOutput().getChoices().get(0).getMessage().getReasoningContent();
        String content = message.getOutput().getChoices().get(0).getMessage().getContent();

        if (!reasoning.isEmpty()) {
            reasoningContent.append(reasoning);
                System.out.println("====================思考过程====================");

            System.out.print(reasoning);
        }

        if (!content.isEmpty()) {
            finalContent.append(content);
                System.out.println("\n====================完整回复====================");
            System.out.print(content);
        }
    }
}
